package com.lovo.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.boot.dao.StudentCourseDAO;
import com.lovo.boot.dao.StudentDAO;
import com.lovo.boot.enums.ResultCode;
import com.lovo.boot.exception.GlobalHandleException;
import com.lovo.boot.model.dos.StudentCourseDO;
import com.lovo.boot.model.dos.StudentDO;
import com.lovo.boot.model.dto.StudentCourseDTO;
import com.lovo.boot.model.dto.StudentDTO;
import com.lovo.boot.model.query.StudentQuery;
import com.lovo.boot.model.vo.PageVO;
import com.lovo.boot.service.StudentService;
import com.lovo.boot.util.PackPageUtil;
import com.lovo.boot.util.PojoTransferUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luo
 * @since 2020-11-25
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDAO, StudentDO> implements StudentService {
    @Resource
    private StudentDAO studentDAO;

    @Resource
    private StudentCourseDAO studentCourseDAO;

    @Override
    public StudentDO findByStudentId(String studentId) throws GlobalHandleException {
        StudentDO studentDO = studentDAO.selectById(studentId);
        if (studentDO == null) {
            throw new GlobalHandleException(ResultCode.STUDENT_FIND_ERROR);
        }
        return studentDO;
    }

    @Override
    public void deleteStudent(String studentId) throws GlobalHandleException {
        int i = studentDAO.deleteById(studentId);
        if (i<=0) {
            throw new GlobalHandleException(ResultCode.STUDENT_DELETE_ERROR);
        }
    }

    @Override
    public StudentDO addStudent(StudentDTO studentDTO) throws GlobalHandleException {
        StudentDO studentDO = PojoTransferUtil.transfer(studentDTO,StudentDO.class);
        int insert = studentDAO.insert(studentDO);
        if (insert<=0) {
            throw new GlobalHandleException(ResultCode.STUDENT_UPDATE_ERROR);
        }
        return studentDO;
    }

    @Override
    public PageVO<StudentDO> findAllStudent(StudentQuery studentQuery) {
        Page<StudentDO> page = new Page<>(studentQuery.getPageNum() == null?1:studentQuery.getPageNum(),
                studentQuery.getPageSize() == null?5:studentQuery.getPageSize());
        LambdaQueryWrapper<StudentDO> wrapper = new QueryWrapper<StudentDO>().lambda().like(StudentDO::getStudentName, studentQuery.getStudentName())
                .like(StudentDO::getStudentGender, studentQuery.getStudentGender());
        Page<StudentDO> selectPage = studentDAO.selectPage(page, wrapper);
        return PackPageUtil.packPage(selectPage);
    }

    @Override
    public StudentDO updateStudent(StudentDTO studentDTO) throws GlobalHandleException {
        StudentDO studentDO = PojoTransferUtil.transfer(studentDTO,StudentDO.class);
        int i = studentDAO.updateById(studentDO);
        if (i<=0) {
            throw new GlobalHandleException(ResultCode.STUDENT_INSERT_ERROR);
        }
        return studentDO;
    }

    @Override
    public void updateStudentCourse(StudentCourseDTO studentCourseDTO) throws GlobalHandleException {
        String  studentId = studentCourseDTO.getStudentId();
        List<String> courseIds = studentCourseDTO.getCourseIds();

        studentCourseDAO.delete(new QueryWrapper<StudentCourseDO>()
                .lambda().eq(StudentCourseDO::getStudentId,studentId));

        if (courseIds.size()<=0) {
            return;
        }

        int i = studentCourseDAO.updateStudentCourse(studentId, courseIds);
        if (i<=0) {
            throw new GlobalHandleException(ResultCode.STUDENT_UPDATE_COURSE_ERROR);
        }

    }
}
